package labox.innovation.gameserver.network.clientpackets;

import java.nio.BufferUnderflowException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import labox.innovation.config.Config;
import labox.innovation.gameserver.handler.ChatHandler;
import labox.innovation.gameserver.handler.IChatHandler;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * This class ...
 *
 * @version $Revision: 1.16.2.12.2.7 $ $Date: 2005/04/11 10:06:11 $
 */
public final class Say2 extends L2GameClientPacket {
	private static final String _C__38_SAY2 = "[C] 38 Say2";

	private static final Logger _log = LoggerFactory.getLogger(Say2.class.getName());

	/**私聊频道 */
	public final static byte SECRET_CHANNAL = 0x00;

	/**场景频道*/
	public final static byte SCENE_CHANNAL = 0x01;

	/**队伍频道*/
	public final static byte PART_CHANNAL = 0x02;

	/**帮会频道*/
	public final static byte CLAN_CHANNAL = 0x03;

	/**种族频道*/
	public final static byte RACE_CHANNAL = 0x04;

	/**世界频道*/
	public final static byte WORLD_CHANNAL = 0x05;

	/**公告频道I*/
	public final static byte NOTICE_CHANNALI = 0x06;

	/**群落频道*/
	public final static byte COMMUNITY_CHANNAL = 0x08;

	/**GM频道*/
	public final static byte GM_CHANNEL = 0x09;
	
	/**系统消息频道 */
	public final static byte SYSTEM_CHANNEL = 0x0a;

	private String _text;

	private byte _type;

	private long _targetID;

	private FuzePcInstance _target;

	private final static String[] CHAT_NAMES = { "SECRET_CHANNAL", "SCENE_CHANNAL", "PART_CHANNAL", "CLAN_CHANNAL", "RACE_CHANNAL", "WORLD_CHANNAL", "NOTICE_CHANNALI", "NOTICE_CHANNALII", "COMMUNITY_CHANNAL", "GM_CHANNEL" };

	@Override
	protected void readImpl() {
		_text = readString(); //-- 聊天内容
		try {
			_type = readByte(); //-- 聊天类别
		} catch (BufferUnderflowException e) {
			_type = (byte) CHAT_NAMES.length;
		}

		if (_type == SECRET_CHANNAL) { //-- 私聊
			_targetID = readLong();
			_target = FuzeWorld.getInstance().getPlayer(_targetID);
		}
	}

	@Override
	protected void runImpl() {
		if (_log.isDebugEnabled())
			_log.debug("Say2: Msg Type = '" + _type + "' Text = '" + _text + "'.", Config.SERVER_ID, "聊天");

		if (_type < 0 || _type >= CHAT_NAMES.length) {
			_log.warn("Say2: Invalid type: " + _type, Config.SERVER_ID, "聊天");
			return;
		}

		FuzePcInstance activeChar = getClient().getActiveChar();
		if (activeChar == null) {
			_log.warn("[Say2.java] Active Character is null.", Config.SERVER_ID, "聊天");
			return;
		}
		
		if ((System.currentTimeMillis() - activeChar.getLastChatTime()) <= Config.FLOOD_PROTECTOR_GLOBAL_CHAT.FLOOD_PROTECTION_INTERVAL) { //-- 聊天频率保护
			activeChar.sendPacket(new SystemMessage(SystemMessageId.FLOOD_PROTECTOR_CHAT));  //-- 往客户端发提示消息
//			activeChar.setLastChatTime(System.currentTimeMillis());  //-- 这段代码暂时屏蔽，以上次发送成功的时间为准
			return;
		}
		
		activeChar.setLastChatTime(System.currentTimeMillis());  //-- 记录本次聊天时间

		if (_text.isEmpty()) {
			_log.warn(activeChar.getName() + ": sending empty text. Possible packet hack!", Config.SERVER_ID, "聊天");
			return;
		}

		// Even though the client can handle more characters than it's current limit allows, an overflow (critical error) happens if you pass a huge (1000+) message.
		// April 27, 2009 - Verified on Gracia P2 & Final official client as 105
		if (_text.length() > 105) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.DONT_SPAM));
			return;
		}

		if (activeChar.getPunish().isChatBanned()) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.CHATTING_IS_CURRENTLY_PROHIBITED));
			return;
		}

		if (activeChar.getPunish().isInJail() && Config.JAIL_DISABLE_CHAT) {
			activeChar.sendMessage("你不能和监狱外面的玩家聊天.");
			return;
		}

		IChatHandler handler = ChatHandler.getInstance().getChatHandler(_type);
		if (handler != null) {
			handler.handleChat(_type, activeChar, _target, _text);
		} else {
			_log.warn("chat Handler is null", Config.SERVER_ID, "聊天");
		}
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__38_SAY2;
	}
}
